home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow / BaseClasses / combase.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  7.4 KB  |  255 lines

  1. //------------------------------------------------------------------------------
  2. // File: ComBase.cpp
  3. //
  4. // Desc: DirectShow base classes - implements class hierarchy for creating
  5. //       COM objects.
  6. //
  7. // Copyright (c) 1992-2001 Microsoft Corporation.  All rights reserved.
  8. //------------------------------------------------------------------------------
  9.  
  10.  
  11. #include <streams.h>
  12. #pragma warning( disable : 4514 )   // Disable warnings re unused inline functions
  13.  
  14.  
  15. /* Define the static member variable */
  16.  
  17. LONG CBaseObject::m_cObjects = 0;
  18.  
  19.  
  20. /* Constructor */
  21.  
  22. CBaseObject::CBaseObject(const TCHAR *pName)
  23. {
  24.     /* Increment the number of active objects */
  25.     InterlockedIncrement(&m_cObjects);
  26.  
  27. #ifdef DEBUG
  28.  
  29. #ifdef UNICODE
  30.     m_dwCookie = DbgRegisterObjectCreation(0, pName);
  31. #else
  32.     m_dwCookie = DbgRegisterObjectCreation(pName, 0);
  33. #endif
  34.  
  35. #endif
  36. }
  37.  
  38. #ifdef UNICODE
  39. CBaseObject::CBaseObject(const char *pName)
  40. {
  41.     /* Increment the number of active objects */
  42.     InterlockedIncrement(&m_cObjects);
  43.  
  44. #ifdef DEBUG
  45.     m_dwCookie = DbgRegisterObjectCreation(pName, 0);
  46. #endif
  47. }
  48. #endif
  49.  
  50. HINSTANCE   hlibOLEAut32;
  51.  
  52. /* Destructor */
  53.  
  54. CBaseObject::~CBaseObject()
  55. {
  56.     /* Decrement the number of objects active */
  57.     if (InterlockedDecrement(&m_cObjects) == 0) {
  58.     if (hlibOLEAut32) {
  59.         FreeLibrary(hlibOLEAut32);
  60.         hlibOLEAut32 = 0;
  61.     }
  62.     };
  63.  
  64.  
  65. #ifdef DEBUG
  66.     DbgRegisterObjectDestruction(m_dwCookie);
  67. #endif
  68. }
  69.  
  70. static const TCHAR szOle32Aut[]   = TEXT("OleAut32.dll");
  71.  
  72. HINSTANCE LoadOLEAut32()
  73. {
  74.     if (hlibOLEAut32 == 0) {
  75.         hlibOLEAut32 = LoadLibrary(szOle32Aut);
  76.     }
  77.  
  78.     return hlibOLEAut32;
  79. }
  80.  
  81.  
  82. /* Constructor */
  83.  
  84. // We know we use "this" in the initialization list, we also know we don't modify *phr.
  85. #pragma warning( disable : 4355 4100 )
  86. CUnknown::CUnknown(const TCHAR *pName, LPUNKNOWN pUnk)
  87. : CBaseObject(pName)
  88. /* Start the object with a reference count of zero - when the      */
  89. /* object is queried for it's first interface this may be          */
  90. /* incremented depending on whether or not this object is          */
  91. /* currently being aggregated upon                                 */
  92. , m_cRef(0)
  93. /* Set our pointer to our IUnknown interface.                      */
  94. /* If we have an outer, use its, otherwise use ours.               */
  95. /* This pointer effectivly points to the owner of                  */
  96. /* this object and can be accessed by the GetOwner() method.       */
  97. , m_pUnknown( pUnk != 0 ? pUnk : reinterpret_cast<LPUNKNOWN>( static_cast<PNDUNKNOWN>(this) ) )
  98.  /* Why the double cast?  Well, the inner cast is a type-safe cast */
  99.  /* to pointer to a type from which we inherit.  The second is     */
  100.  /* type-unsafe but works because INonDelegatingUnknown "behaves   */
  101.  /* like" IUnknown. (Only the names on the methods change.)        */
  102. {
  103.     // Everything we need to do has been done in the initializer list
  104. }
  105.  
  106. // This does the same as above except it has a useless HRESULT argument
  107. // use the previous constructor, this is just left for compatibility...
  108. CUnknown::CUnknown(TCHAR *pName, LPUNKNOWN pUnk,HRESULT *phr) :
  109.     CBaseObject(pName),
  110.     m_cRef(0),
  111.     m_pUnknown( pUnk != 0 ? pUnk : reinterpret_cast<LPUNKNOWN>( static_cast<PNDUNKNOWN>(this) ) )
  112. {
  113. }
  114.  
  115. #ifdef UNICODE
  116. CUnknown::CUnknown(const CHAR *pName, LPUNKNOWN pUnk)
  117. : CBaseObject(pName), m_cRef(0),
  118.     m_pUnknown( pUnk != 0 ? pUnk : reinterpret_cast<LPUNKNOWN>( static_cast<PNDUNKNOWN>(this) ) )
  119. { }
  120.  
  121. CUnknown::CUnknown(CHAR *pName, LPUNKNOWN pUnk,HRESULT *phr) :
  122.     CBaseObject(pName), m_cRef(0),
  123.     m_pUnknown( pUnk != 0 ? pUnk : reinterpret_cast<LPUNKNOWN>( static_cast<PNDUNKNOWN>(this) ) )
  124. { }
  125.  
  126. #endif
  127.  
  128. #pragma warning( default : 4355 4100 )
  129.  
  130.  
  131. /* QueryInterface */
  132.  
  133. STDMETHODIMP CUnknown::NonDelegatingQueryInterface(REFIID riid, void ** ppv)
  134. {
  135.     CheckPointer(ppv,E_POINTER);
  136.     ValidateReadWritePtr(ppv,sizeof(PVOID));
  137.  
  138.     /* We know only about IUnknown */
  139.  
  140.     if (riid == IID_IUnknown) {
  141.         GetInterface((LPUNKNOWN) (PNDUNKNOWN) this, ppv);
  142.         return NOERROR;
  143.     } else {
  144.         *ppv = NULL;
  145.         return E_NOINTERFACE;
  146.     }
  147. }
  148.  
  149. /* We have to ensure that we DON'T use a max macro, since these will typically   */
  150. /* lead to one of the parameters being evaluated twice.  Since we are worried    */
  151. /* about concurrency, we can't afford to access the m_cRef twice since we can't  */
  152. /* afford to run the risk that its value having changed between accesses.        */
  153.  
  154. template<class T> inline static T ourmax( const T & a, const T & b )
  155. {
  156.     return a > b ? a : b;
  157. }
  158.  
  159. /* AddRef */
  160.  
  161. STDMETHODIMP_(ULONG) CUnknown::NonDelegatingAddRef()
  162. {
  163.     LONG lRef = InterlockedIncrement( &m_cRef );
  164.     ASSERT(lRef > 0);
  165.     DbgLog((LOG_MEMORY,3,TEXT("    Obj %d ref++ = %d"),
  166.            m_dwCookie, m_cRef));
  167.     return ourmax(ULONG(m_cRef), 1ul);
  168. }
  169.  
  170.  
  171. /* Release */
  172.  
  173. STDMETHODIMP_(ULONG) CUnknown::NonDelegatingRelease()
  174. {
  175.     /* If the reference count drops to zero delete ourselves */
  176.  
  177.     LONG lRef = InterlockedDecrement( &m_cRef );
  178.     ASSERT(lRef >= 0);
  179.  
  180.     DbgLog((LOG_MEMORY,3,TEXT("    Object %d ref-- = %d"),
  181.         m_dwCookie, m_cRef));
  182.     if (lRef == 0) {
  183.  
  184.         // COM rules say we must protect against re-entrancy.
  185.         // If we are an aggregator and we hold our own interfaces
  186.         // on the aggregatee, the QI for these interfaces will
  187.         // addref ourselves. So after doing the QI we must release
  188.         // a ref count on ourselves. Then, before releasing the
  189.         // private interface, we must addref ourselves. When we do
  190.         // this from the destructor here it will result in the ref
  191.         // count going to 1 and then back to 0 causing us to
  192.         // re-enter the destructor. Hence we add an extra refcount here
  193.         // once we know we will delete the object.
  194.         // for an example aggregator see filgraph\distrib.cpp.
  195.  
  196.         m_cRef++;
  197.  
  198.         delete this;
  199.         return ULONG(0);
  200.     } else {
  201.         return ourmax(ULONG(m_cRef), 1ul);
  202.     }
  203. }
  204.  
  205.  
  206. /* Return an interface pointer to a requesting client
  207.    performing a thread safe AddRef as necessary */
  208.  
  209. STDAPI GetInterface(LPUNKNOWN pUnk, void **ppv)
  210. {
  211.     CheckPointer(ppv, E_POINTER);
  212.     *ppv = pUnk;
  213.     pUnk->AddRef();
  214.     return NOERROR;
  215. }
  216.  
  217.  
  218. /* Compares two interfaces and returns TRUE if they are on the same object */
  219.  
  220. BOOL WINAPI IsEqualObject(IUnknown *pFirst, IUnknown *pSecond)
  221. {
  222.     /*  Different objects can't have the same interface pointer for
  223.         any interface
  224.     */
  225.     if (pFirst == pSecond) {
  226.         return TRUE;
  227.     }
  228.     /*  OK - do it the hard way - check if they have the same
  229.         IUnknown pointers - a single object can only have one of these
  230.     */
  231.     LPUNKNOWN pUnknown1;     // Retrieve the IUnknown interface
  232.     LPUNKNOWN pUnknown2;     // Retrieve the other IUnknown interface
  233.     HRESULT hr;              // General OLE return code
  234.  
  235.     ASSERT(pFirst);
  236.     ASSERT(pSecond);
  237.  
  238.     /* See if the IUnknown pointers match */
  239.  
  240.     hr = pFirst->QueryInterface(IID_IUnknown,(void **) &pUnknown1);
  241.     ASSERT(SUCCEEDED(hr));
  242.     ASSERT(pUnknown1);
  243.  
  244.     hr = pSecond->QueryInterface(IID_IUnknown,(void **) &pUnknown2);
  245.     ASSERT(SUCCEEDED(hr));
  246.     ASSERT(pUnknown2);
  247.  
  248.     /* Release the extra interfaces we hold */
  249.  
  250.     pUnknown1->Release();
  251.     pUnknown2->Release();
  252.     return (pUnknown1 == pUnknown2);
  253. }
  254.  
  255.